package com.kg.fiber.common;

import java.util.Arrays;

import lombok.Getter;

public class DoubleBuffer {

	private double[] data;
	
	@Getter
	private int size;

	public DoubleBuffer(int capacity) {
		capacity = Math.max(256, capacity);
		data = new double[capacity];
	}
	
	public boolean empty() {
		return size == 0;
	}
	
	public int capacity() {
		if(data == null) return 0;
		return data.length;
	}
	
	public void add(double value) {
		
		if((size + 1) > data.length) {		
			System.out.println("DoubleBuffer increase to " + (data.length * 2));
			data = Arrays.copyOf(data, data.length * 2);
		}
		
		data[size] = value;
		size++;
		
	}
	
	public double get(int idx) {
		
		return data[idx];
		
	}
	
	public void clear() {
		size = 0;
	}
	
	public void eat(int n) {
		
		if(n <= 0) return;
		
		if(n >= size) {
			
			clear();
			
		} else {
			
			System.arraycopy(data, n, data, 0, size - n);
			
			size -= n;
			
		}
		
	}
	
	
}
